package com.xiaoyg.thread;

import java.util.concurrent.atomic.AtomicInteger;

/***
 * 计算机内部计算，减法转换为加法运算，
 * 原码：二进制表示形式，第一位位标志位，0表示正数，1表示复数
 *  14：正数的反码，反码，补码相同
 *  原码：00001110
 *  反码：同原码
 *  补码：同原码
 *  -21 源码转反码，符号位不变，其他取反
 *  补码：反码+1
 *  原码：10010101
 *  反码：11101010
 *  补码：11101011
 *
 *  计算：14+（-21）
 *  补码相加：00001110 + 11101011 = 11111001
 *  转反码：补码-1 =》 11111000
 *  再转原码：标志位不变，其它位取反 10000111
 *  转换为十进制：-7
 *
 *
 *
 * 补码 + 补码 = 补码
 * 补码 -》 反码 -》 源码，最终得到数据。
 */
public class CtlTest {

    private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));
    // 29位
    private static final int COUNT_BITS = Integer.SIZE - 3;

    //1左移29位 -1 = 111111111111111111111111111111
    private static final int CAPACITY   = (1 << COUNT_BITS) - 1;

    // runState is stored in the high-order bits
    // rs < SHUTDOWN 表示线程池运行状态
    //左移29位
    private static final int RUNNING    =  1 << COUNT_BITS;
    private static final int SHUTDOWN   =  0 << COUNT_BITS;
    private static final int STOP       =  1 << COUNT_BITS;
    private static final int TIDYING    =  2 << COUNT_BITS;
    private static final int TERMINATED =  3 << COUNT_BITS;

    // Packing and unpacking ctl
    private static int runStateOf(int c)     { return c & ~CAPACITY; }
    private static int workerCountOf(int c)  { return c & CAPACITY; }
    private static int ctlOf(int rs, int wc) { return rs | wc; }

    /*
     * Bit field accessors that don't require unpacking ctl.
     * These depend on the bit layout and on workerCount being never negative.
     */

    private static boolean runStateLessThan(int c, int s) {
        return c < s;
    }

    private static boolean runStateAtLeast(int c, int s) {
        return c >= s;
    }

    private static boolean isRunning(int c) {
        return c < SHUTDOWN;
    }

    /**
     * Attempts to CAS-increment the workerCount field of ctl.
     */
    private boolean compareAndIncrementWorkerCount(int expect) {
        return ctl.compareAndSet(expect, expect + 1);
    }

    /**
     * Attempts to CAS-decrement the workerCount field of ctl.
     */
    private boolean compareAndDecrementWorkerCount(int expect) {
        return ctl.compareAndSet(expect, expect - 1);
    }

    /**
     * Decrements the workerCount field of ctl. This is called only on
     * abrupt termination of a thread (see processWorkerExit). Other
     * decrements are performed within getTask.
     */
    private void decrementWorkerCount() {
        do {} while (! compareAndDecrementWorkerCount(ctl.get()));
    }

    public static void main(String[] args) {
        System.out.println("RUNNING:" +Integer.toBinaryString(RUNNING));
        System.out.println("SHUTDOWN:" +Integer.toBinaryString(SHUTDOWN));
        System.out.println("STOP:" +Integer.toBinaryString(STOP));
        System.out.println("TIDYING:" +Integer.toBinaryString(TIDYING));
        System.out.println("TERMINATED:" +Integer.toBinaryString(TERMINATED));
    }

}
